﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebStudio.Mvc.Core.Extensions
{
    /// <summary>
    /// 图片控件
    /// </summary>
    public static class ImageExtension
    {
        /// <summary>
        /// return image link
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id">Id of link control</param>
        /// <param name="controller">target controller name</param>
        /// <param name="action">target action name</param>
        /// <param name="strOthers">other URL parts like querystring, etc</param>
        /// <param name="strImageURL">URL for image</param>
        /// <param name="alternateText">Alternate Text for the image</param>
        /// <param name="strStyle">style of the image like border properties, etc</param>
        /// <returns></returns>
        public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL, string alternateText, string strStyle)
        {
            return ImageLink(helper, id, controller, action, strOthers, strImageURL, alternateText,strStyle,null);
        }
        /// <summary>
        /// return image link
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id">Id of link control</param>
        /// <param name="controller">target controller name</param>
        /// <param name="action">target action name</param>
        /// <param name="strOthers">other URL parts like querystring, etc</param>
        /// <param name="strImageURL">URL for image</param>
        /// <param name="alternateText">Alternate Text for the image</param>
        /// <param name="strStyle">style of the image like border properties, etc</param>
        /// <param name="htmlAttributes">html attribues for link</param>
        /// <returns></returns>
        public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL, string alternateText,string strStyle, object htmlAttributes)
        {
            // Create tag builder
            var builder = new TagBuilder("a");

            // Create valid id
            builder.GenerateId(id);

            // Add attributes
            builder.MergeAttribute("href", "/" + controller + "/" + action + strOthers); //form target URL
            builder.InnerHtml = "<img src='" + strImageURL + "' alt='" + alternateText + "' style=\"" + strStyle + "\">"; //set the image as inner html
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            // Render tag
            return builder.ToString(TagRenderMode.Normal); //to add </a> as end tag
        }
    }
}
